package com.quan.whizenoa.data.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.pm.commons.vo.PmProjectCenterVo;
import com.quan.pm.entity.PmProjectCenter;
import com.quan.pm.service.PmProjectCenterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.quan.whizenoa.data.commons.vo.IeasyOaProjectCenterVo;
import com.quan.whizenoa.data.entity.IeasyOaProjectCenter;
import com.quan.whizenoa.data.mapper.IeasyOaProjectCenterMapper;
import com.quan.whizenoa.data.service.IeasyOaProjectCenterService;


@Slf4j
@Service
@DS("ieasy")
public class IeasyOaProjectCenterServiceImpl extends MyBaseServiceImpl<IeasyOaProjectCenterMapper, IeasyOaProjectCenter> implements IeasyOaProjectCenterService {

    @Autowired
    private IeasyOaProjectCenterMapper mapper;

    @Autowired
    private PmProjectCenterService projectCenterService;

    @Override
    public List<IeasyOaProjectCenterVo> importIeasyProject2PmgProject() {

        final List<IeasyOaProjectCenter> list = super.list();
        for (IeasyOaProjectCenter p : list) {

            PmProjectCenter pm = new PmProjectCenter();
            pm.setDeptName(p.getProjOwnerDeptName());
            pm.setDeptOwnerName(p.getProjOwnerName());
            pm.setProjectMgrName(p.getProjManagerNames());
            pm.setName(p.getProjName());
            pm.setCode(p.getProjNum());
            pm.setStartDate(p.getProjStartTime());
            pm.setEndDate(p.getProjEndTime());
            pm.setStatus(p.getProjStatus()+"");

            this.projectCenterService.save(pm);
        }

        return null;
    }

    @Override
    public List<IeasyOaProjectCenterVo> queryList(PageUtils pageUtils, IeasyOaProjectCenterVo vo) {
        QueryWrapper<IeasyOaProjectCenter> queryWrapper = new QueryWrapper<IeasyOaProjectCenter>(vo);

        List<IeasyOaProjectCenter> list = super.list(pageUtils, queryWrapper);
        List<IeasyOaProjectCenterVo> collect = list.stream().map(item -> {
                IeasyOaProjectCenterVo bean = new IeasyOaProjectCenterVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, IeasyOaProjectCenterVo vo) {
        QueryWrapper<IeasyOaProjectCenter> queryWrapper = new QueryWrapper<IeasyOaProjectCenter>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<IeasyOaProjectCenter> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<IeasyOaProjectCenter> records = page.getRecords();

        final List<IeasyOaProjectCenter> collect = records.stream().map((item) -> {
                IeasyOaProjectCenterVo bean = new IeasyOaProjectCenterVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}